#region Copyright & License
/* Tyng.Data
 * Copyright (c) 2007, Paul Tyng, All rights reserved.
 * Code licensed under the BSD License:
 * http://www.opensource.org/licenses/bsd-license.php
 * Downloaded from: http://code.google.com/p/tyngdata/
*/
#endregion

using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.IO;

namespace Tyng.Data
{
    /// <summary>
    /// A DataReader for streams, allowing for multiple types of records in a single file. 
    /// </summary>
    /// <remarks>Derived classes are responsible for implementing the IDataRecord objects, and for detecting 
    /// which record to use.</remarks>
    public abstract class TextReaderDataReader : CustomDataReader
    {
        private bool _closeReader = true;
        private TextReader _reader;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="reader">TextReader for data</param>
        /// <param name="closeReader">Flag indicating whether or not to close the stream when the Close method is called</param>
        protected TextReaderDataReader(TextReader reader, bool closeReader)
            : this(reader)
        {
            _closeReader = closeReader;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="reader">TextReader for data</param>
        protected TextReaderDataReader(TextReader reader)
        {
            _reader = reader;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="reader">TextReader for data</param>
        /// <param name="maxRowsToRead">Maximum number of rows to read from the reader</param>
        protected TextReaderDataReader(TextReader reader, int maxRowsToRead)
            : base(maxRowsToRead)
        {
            _reader = reader;
        }

        /// <summary>
        /// Access to the TextReader.
        /// </summary>
        protected TextReader TextReader
        {
            get
            {
                return _reader;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the data reader is closed.
        /// </summary>
        public override bool IsClosed
        {
            get
            {
                return _reader == null;
            }
        }

        /// <summary>
        /// Closes the StreamDataReader
        /// </summary>
        public override void Close()
        {
            base.Close();

            if (_reader != null && _closeReader)
                _reader.Close();

            _reader = null;
        }

        /// <summary>
        /// Advances the StreamDataReader to the next record
        /// </summary>
        /// <returns>Boolean indicating whether or not the next record exists</returns>
        public override bool Read()
        {
            if (_reader == null)
                return false;

            return base.Read();
        }
    }
}
